﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils.Menu;

namespace DevDemo
{
    public partial class UC_DrugLists : DevExpress.XtraEditors.XtraUserControl
    {
        public UC_DrugLists()
        {
            InitializeComponent();
        }

        public string TypeName = null;

        TableLayoutPanel tb = new TableLayoutPanel();

        private string SelectControlTagID = "";

        public List<DrugClass> drugobjs = new List<DrugClass>();

        public delegate void SetDeleteControls(string TypeCode);

        public event SetDeleteControls SetDelMethod;

        private void UC_DrugLists_Load(object sender, EventArgs e)
        {
            this.lb_typeName.Text = TypeName;

            #region BeforeCode

            //int inty = 0;

            //foreach (var item in drugobjs)
            //{
            //    var obj = new UC_DrugList();

            //    obj.DrugObj = item;

            //    obj.Location = new Point(0, inty);

            //    this.panel1.Controls.Add(obj);

            //    inty += 50;
            //}

            #endregion

            #region TableLayOutPanel

            GetTableLayOutData();

            #endregion

            #region AddDropDownButton

            DropDownButtonShow();

            #endregion
        }

        /// <summary>
        /// 
        /// </summary>
        public void GetTableLayOutData()
        {
            TableLayoutPanel table = new TableLayoutPanel();

            this.panel1.Controls.Clear();

            // 默认添加一行数据
            table.Dock = DockStyle.Top;

            panel1.Controls.Add(table);

            table.ColumnCount = 1;

            table.Height = table.RowCount * 80;

            int step = 100 / table.ColumnCount;

            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, step));

            for (int ii = 0; ii < table.RowCount; ii++)
            {
                table.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40));
            }

            int rows = 0;

            foreach (var item in drugobjs)
            {
                var obj = new UC_DrugList();

                obj.DrugObj = item;

                obj.Tag = item.DrugID;

                //动态添加一行
                table.RowCount++;

                //设置高度
                table.Height = table.RowCount * 65;

                //行高
                table.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 60));

                //设置cell样式，
                table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

                obj.MouseDown += new MouseEventHandler(gb_MouseDown);

                obj.MouseDoubleClick += new MouseEventHandler(obj_MouseDoubleClick);

                table.Controls.Add(obj, 0, rows);

                rows++;
            }

            tb = table;
        }

        /// <summary>
        /// 右键点击获取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                var obj = (UC_DrugList)sender;

                SelectControlTagID = obj.Tag.ToString();
            }
        }

        /// <summary>
        /// 双击项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void obj_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SelectControlTagID))
            {
                RemoveControls(this.panel1, tb, SelectControlTagID);
            }
        }

        /// <summary>
        /// 删除控件方法 row
        /// </summary>
        /// <param name="items"></param>
        /// <param name="tagId"></param>
        public void RemoveControls(Control items, TableLayoutPanel table, string tagId)
        {
            foreach (var item in items.Controls)
            {
                if (item.GetType().ToString().ToUpper() == "DEVDEMO.UC_DRUGLIST")
                {
                    var b = (UC_DrugList)item;

                    if (b.Tag.ToString() == tagId)
                    {
                        int rowIndex = table.GetRow(b);

                        table.Controls.RemoveAt(rowIndex);

                        // 移动,当前行row的下行往上移动
                        for (int k = rowIndex; k < table.RowCount - 1; k++)
                        {
                            Control ctlNext = table.GetControlFromPosition(0, k + 1);

                            table.SetCellPosition(ctlNext, new TableLayoutPanelCellPosition(0, k));
                        }

                        table.RowStyles.RemoveAt(table.RowCount - 1);

                        table.RowCount = table.RowCount - 1;

                        break;
                    }
                }
                else
                {
                    RemoveControls((Control)item, table, tagId);
                }
            }

            // 重新计算高度，否则最后一行偏大
            table.Height = table.RowCount * 80;
        }

        /// <summary>
        /// 加载操作
        /// </summary>
        public void DropDownButtonShow()
        {
            DXPopupMenu menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("新增", OnItemAddClick));
            menu.Items.Add(new DXMenuItem("修改", OnItemEditClick));
            menu.Items.Add(new DXMenuItem("删除", OnItemDelClick));
            this.dpdb_Edit.DropDownControl = menu;
        }

        private void OnItemAddClick(object sender, EventArgs e)
        {
            DXMenuItem item = sender as DXMenuItem;

            XtraMessageBox.Show("Add" + item.Caption);
        }

        private void OnItemEditClick(object sender, EventArgs e)
        {
            DXMenuItem item = sender as DXMenuItem;

            XtraMessageBox.Show("Edit" + item.Caption);
        }

        private void OnItemDelClick(object sender, EventArgs e)
        {
            //DXMenuItem item = sender as DXMenuItem;

            //XtraMessageBox.Show("Del" + item.Caption);

            SetDelMethod(this.Tag.ToString());
        }
    }
}
